package com.turnengine.client.local.action.bean;

import com.turnengine.client.local.action.enums.ActionConditionExecute;
import com.turnengine.client.local.action.enums.ActionConditionMandatory;
import com.turnengine.client.local.action.enums.ActionConditionType;
import com.turnengine.client.local.action.enums.ActionTargetType;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Action Condition.
 */
public class ActionCondition implements IActionCondition, Comparable<IActionCondition> {

	/** The action id. */
	private int actionId = -1;
	/** The target type. */
	private ActionTargetType targetType = ActionTargetType.SOURCE;
	/** The type. */
	private ActionConditionType type = ActionConditionType.EXISTS_ONCE;
	/** The execute. */
	private ActionConditionExecute execute = ActionConditionExecute.START;
	/** The mandatory. */
	private ActionConditionMandatory mandatory = ActionConditionMandatory.MANDATORY;
	/** The condition id. */
	private int conditionId = -1;
	/** The condition amount. */
	private long conditionAmount = 0;

	/**
	 * Creates a new Action Condition.
	 */
	public ActionCondition() {
	}

	/**
	 * Creates a new Action Condition.
	 * @param actionId the action id
	 * @param targetType the target type
	 * @param type the type
	 * @param execute the execute
	 * @param mandatory the mandatory
	 * @param conditionId the condition id
	 * @param conditionAmount the condition amount
	 */
	public ActionCondition(int actionId, ActionTargetType targetType, ActionConditionType type, ActionConditionExecute execute, ActionConditionMandatory mandatory, int conditionId, long conditionAmount) {
		setActionId(actionId);
		setTargetType(targetType);
		setType(type);
		setExecute(execute);
		setMandatory(mandatory);
		setConditionId(conditionId);
		setConditionAmount(conditionAmount);
	}

	/**
	 * Creates a new Action Condition.
	 * @param actionCondition the action condition
	 */
	public ActionCondition(ActionCondition actionCondition) {
		setActionId(actionCondition.getActionId());
		setTargetType(actionCondition.getTargetType());
		setType(actionCondition.getType());
		setExecute(actionCondition.getExecute());
		setMandatory(actionCondition.getMandatory());
		setConditionId(actionCondition.getConditionId());
		setConditionAmount(actionCondition.getConditionAmount());
	}

	/**
	 * Creates a new Action Condition.
	 * @param iActionCondition the i action condition
	 */
	public ActionCondition(IActionCondition iActionCondition) {
		setActionId(iActionCondition.getActionId());
		setTargetType(iActionCondition.getTargetType());
		setType(iActionCondition.getType());
		setExecute(iActionCondition.getExecute());
		setMandatory(iActionCondition.getMandatory());
		setConditionId(iActionCondition.getConditionId());
		setConditionAmount(iActionCondition.getConditionAmount());
	}

	/**
	 * Returns the action id.
	 * @return the action id.
	 */
	public int getActionId() {
		return actionId;
	}

	/**
	 * Returns the target type.
	 * @return the target type.
	 */
	public ActionTargetType getTargetType() {
		return targetType;
	}

	/**
	 * Returns the type.
	 * @return the type.
	 */
	public ActionConditionType getType() {
		return type;
	}

	/**
	 * Returns the execute.
	 * @return the execute.
	 */
	public ActionConditionExecute getExecute() {
		return execute;
	}

	/**
	 * Returns the mandatory.
	 * @return the mandatory.
	 */
	public ActionConditionMandatory getMandatory() {
		return mandatory;
	}

	/**
	 * Returns the condition id.
	 * @return the condition id.
	 */
	public int getConditionId() {
		return conditionId;
	}

	/**
	 * Returns the condition amount.
	 * @return the condition amount.
	 */
	public long getConditionAmount() {
		return conditionAmount;
	}

	/**
	 * Sets the action id.
	 * @param actionId the action id to set.
	 */
	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	/**
	 * Sets the target type.
	 * @param targetType the target type to set.
	 */
	public void setTargetType(ActionTargetType targetType) {
		this.targetType = targetType;
	}

	/**
	 * Sets the type.
	 * @param type the type to set.
	 */
	public void setType(ActionConditionType type) {
		this.type = type;
	}

	/**
	 * Sets the execute.
	 * @param execute the execute to set.
	 */
	public void setExecute(ActionConditionExecute execute) {
		this.execute = execute;
	}

	/**
	 * Sets the mandatory.
	 * @param mandatory the mandatory to set.
	 */
	public void setMandatory(ActionConditionMandatory mandatory) {
		this.mandatory = mandatory;
	}

	/**
	 * Sets the condition id.
	 * @param conditionId the condition id to set.
	 */
	public void setConditionId(int conditionId) {
		this.conditionId = conditionId;
	}

	/**
	 * Sets the condition amount.
	 * @param conditionAmount the condition amount to set.
	 */
	public void setConditionAmount(long conditionAmount) {
		this.conditionAmount = conditionAmount;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(379, 37);
		hash.append(actionId);
		hash.append(targetType);
		hash.append(type);
		hash.append(execute);
		hash.append(mandatory);
		hash.append(conditionId);
		hash.append(conditionAmount);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof ActionCondition) {
			ActionCondition compare = (ActionCondition) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.actionId, compare.actionId);
			equals.append(this.targetType, compare.targetType);
			equals.append(this.type, compare.type);
			equals.append(this.execute, compare.execute);
			equals.append(this.mandatory, compare.mandatory);
			equals.append(this.conditionId, compare.conditionId);
			equals.append(this.conditionAmount, compare.conditionAmount);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IActionCondition compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.actionId, compare.getActionId());
		builder.append(this.targetType, compare.getTargetType());
		builder.append(this.type, compare.getType());
		builder.append(this.execute, compare.getExecute());
		builder.append(this.mandatory, compare.getMandatory());
		builder.append(this.conditionId, compare.getConditionId());
		builder.append(this.conditionAmount, compare.getConditionAmount());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("actionId", actionId);
		if (targetType != null) {
			string.append("targetType", targetType);
		}
		if (type != null) {
			string.append("type", type);
		}
		if (execute != null) {
			string.append("execute", execute);
		}
		if (mandatory != null) {
			string.append("mandatory", mandatory);
		}
		string.append("conditionId", conditionId);
		string.append("conditionAmount", conditionAmount);
		return string.toString();
	}
}
